package com.fc.app.fcapp.db;

import android.content.Context;
import android.support.test.rule.ActivityTestRule;
import android.util.Log;

import com.fc.app.fcapp.CoreFuncKt;
import com.fc.app.fcapp.FCApplication;
import com.fc.app.fcapp.SplashActivity;
import com.fc.app.fcapp.generics.T_Activity;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by Administrator on 2018/2/24.
 */
public class AdDbManagerImplTest {
    @Rule
    public ActivityTestRule mActivityTestRule = new ActivityTestRule(SplashActivity.class);
    private Context mContext;

    @Before
    public void setUp() throws Exception {
        mContext = mActivityTestRule.getActivity().getApplicationContext();
        AdDbManagerImpl.getInstance().setContext(mContext);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void getInstance() throws Exception {
    }

    @Test
    public void insert() throws Exception {
        Log.i("dbTest", "-------deleteAll-----------");
        AdDbManagerImpl.getInstance().deleteAll();
        logdb();
        boolean flag = AdDbManagerImpl.getInstance().insert(null);
        Assert.assertEquals(false, flag);
        flag = AdDbManagerImpl.getInstance().insert(createTestData());
        Assert.assertEquals(true, flag);


        Log.i("dbTest", "-------insert-----------");
        AdDbManagerImpl.getInstance().insert(createTestData());
        AdDbManagerImpl.getInstance().insert(createTestData1());
        logdb();

        int mount = AdDbManagerImpl.getInstance().queryAll().size();
        Assert.assertEquals(2, mount);

        Log.i("dbTest", "-------queryByResUri-----------");
        AdMsgData adMsgData = AdDbManagerImpl.getInstance().queryByResUri("1111111111");
        if (adMsgData == null)
            Log.i("dbTest", "queryByResUri = null");
        else{
            AdDbManagerImpl.getInstance().deleteById(adMsgData.getId());
            logdb();
        }


    }

    @Test
    public void deleteById() throws Exception {
        AdDbManagerImpl.getInstance().deleteAll();
    }

    @Test
    public void queryByResUri() throws Exception {
    }

    @Test
    public void queryAll() throws Exception {
        AdDbManagerImpl.getInstance().deleteAll();
        AdDbManagerImpl.getInstance().insert(createTestData());
        AdDbManagerImpl.getInstance().insert(createTestData1());
        logdb();
    }

    @Test
    public void setContext() throws Exception {
    }


    private AdMsgData createTestData() {
        AdMsgData data = new AdMsgData();
        data.setResUri("http://blog.csdn.net/zjy_hll/article/details/38319837");
        data.setNativeUri("http://blog.csdn.net/zjy_hll/article/details/38319837xxxxxxxxxxxxxxx");
        data.setMd5("123148618461286489164816849");
        data.setAttrOnclick("setAttrOnclick baseData");
        data.setAdID("dabcd");
        data.setAttrDuration(10);
        return data;
    }

    private AdMsgData createTestData1() {
        AdMsgData data = new AdMsgData();
        data.setResUri("1111111111");
        data.setNativeUri("222222222222");
        data.setMd5("333333333333");
        data.setAttrOnclick("44444444444");
        data.setAdID("55555555555");
        data.setAttrDuration(666);
        return data;
    }


    private synchronized void logdb() {
        CoreFuncKt.ioThread(new Runnable() {
            @Override
            public void run() {
                List<AdMsgData> list = AdDbManagerImpl.getInstance().queryAll();
                if (list != null) {
                    Log.i("dbTest", "db.size "+ list.size());
                    for (AdMsgData data : list) {
                        Log.i("dbTest", "id:"+data.getId()+"   data.getAttrDuration():" + data.getAttrDuration() + "  data.getMd5(): " + data.getMd5() + "  data.getResUri():" + data.getResUri());
                    }
                } else {
                    Log.i("dbTest", "db = null");
                }
            }
        });

    }
}